import wepy from 'wepy'
import { api } from './config'

export const requestIntercept = {
	config(p) {
		// wepy.showLoading({
		// 	title:'加载中',
		// 	mask: true
		// })
		return p
	},
	success(p) {
		wepy.hideLoading()
		return p
	},
	fail(p) {
		wepy.hideLoading()
		showErrorMsg('请求失败，请稍后重试')
		return p
	}
}
export function showErrorMsg(msg, title='提醒') {
	wepy.showModal({
		title: title,
		content: msg,
		showCancel: false,
		confirmColor: '#362f7f'
	})
}

export function showSuccessMsg(msg, title='提示') {
	wepy.showModal({
		title: title,
		content: msg,
		showCancel: false
	})
}

export function requestData(options, success, error) {
	wepy.request(options).then(data => {
		if(data.statusCode == 200) {
			success(data.data)
		}else {
			showErrorMsg('http request error:' + data.statusCode)
			error(data)
		}
	})
}

export function formateNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

export function formateLocalDate(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return year + '年' + month + '月' + day + '日'
}
export function formatDate(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return [year, month, day].map(formateNumber).join('-') + ' ' + formateNumber(hour) + ':' + formateNumber(minute)
}

export function formatWeek(n) {
  const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  return weekdays[n]
}

export function formatDateWeek(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const week = date.getDay()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  return month + '月' + day + '日' + ' ' + formatWeek(week)
}